import orderApi from '@/api/shop/order'
import expressApi from '@/api/system/express'
import { getApiUrl } from '@/utils/utils'
import { getToken } from '@/utils/auth'
import settlementApi from '@/api/settlement/settlement'
import permission from '@/directive/permission/index.js'
export default {
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        mobile: undefined,
        orderSn: undefined,
        status: undefined,
        date: undefined,
        startDate: undefined,
        endDate: undefined,
        goodsCreat: 0,
      },
      orderDate: undefined,
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      logVisible: false,
      logs: [],
      expressList: [],
      //上传文件
      importXls: '',
      apiUrl: getApiUrl(),
      uploadHeaders: {
        'Authorization': ''
      },
      goodsCreatList: [],
      goodsCreatSelect: 0,
      initFlag: true,
      //发货表单
      shipping: {
        show: false,
        id: '',
        idExpress: '',
        shippingSn: ''
      },
      //物流信息
      shippingInfo: {
        show: false,
        form: {
          id: '',
          idExpress: '',
          shippingSn: '',
          traces: []
        }
      },
      // 退款信息
      refundInfo: {
        show: false,
        form: {
          username: "",
          orderSn: "",
          statusName: "",
          status: 0,
          totalPrice: 0,
          realPrice: 0,
        },
      },
      query: {
        button: {
          lastStatus: 'all',
          lastDate: 'all',
          showCustomer: false,
          css: {
            status: {
              all: 'primary',
              unPay: 'default',
              unSend: 'default',
              sended: 'default',
              finished: 'default',
              cancel: 'default',
              refundIng: 'default',
              refund: 'default'
            },
            date: {
              all: 'primary',
              today: 'default',
              yesterday: 'default',
              seven: 'default',
              thirty: 'default',
              month: 'default',
              year: 'default',
              customer: 'default'
            }
          },
          tag: {
            unPay: 0,
            unSend: 0,
            sended: 0,
            finished: 0,
            cancel: 0,
            refundIng: 0,
            refund: 0
          }
        }
      }
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  watch: {
    goodsCreatSelect(val, oldVal) {
      console.log('筛选框值变动', val, oldVal);
      this.listQuery.goodsCreat = val
      this.fetchData()
    },
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getGoodsCreatList()
      this.importXls = getApiUrl() + '/shop/order/importexcel'
      this.uploadHeaders['Authorization'] = getToken()
      // this.fetchData()
      orderApi.getOrderStatistic().then(response => {
        const data = response.data
        for (const key in data) {
          this.query.button.tag[key] = data[key]
        }
      })
      expressApi.queryAll().then(response => {
        this.expressList = response.data
      })

    },
    fetchData() {

      this.listLoading = true
      if (this.orderDate) {
        this.listQuery['startDate'] = this.orderDate[0]
        this.listQuery['endDate'] = this.orderDate[1]
      }

      orderApi.getList(this.listQuery).then(response => {
        console.log(response.data.records);
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })

    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.mobile = ''
      this.listQuery.orderSn = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleUploadPicSuccess(response, raw) {
      if (response.code === 20000) {
        this.$message({
          message: this.$t('common.optionSuccess'),
          type: 'success'
        })
      } else {
        this.$message({
          message: this.$t('common.uploadError'),
          type: 'error'
        })
      }
    },
    queryByState(status) {
      const lastStatus = this.query.button.lastStatus
      this.query.button.css.status[lastStatus] = 'default'
      this.query.button.css.status[status] = 'primary'
      this.query.button.lastStatus = status
      if (status !== 'all') {
        this.listQuery.status = status
      } else {
        this.listQuery.status = ''
      }
      this.fetchData()
    },
    queryByDate(date) {
      const lastDate = this.query.button.lastDate
      this.query.button.css.date[lastDate] = 'default'
      this.query.button.css.date[date] = 'primary'
      this.query.button.lastDate = date
      if (date !== 'all' && date !== 'customer') {
        this.listQuery.date = date
      } else {
        this.listQuery.date = ''
      }
      if (date === 'customer') {
        this.query.button.showCustomer = true
        return
      } else {
        this.query.button.showCustomer = false
      }
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    formatPrice(price) {
      return (price / 100).toFixed(2);
    },
    viewShippingInfo(data) {
      this.shippingInfo.form = data
      const expressList = this.expressList
      let shipperCode = ''
      for (const index in expressList) {
        const express = expressList[index]
        if (express.id === data.idExpress) {
          shipperCode = express.code
        }
      }
      orderApi.getShippingInfo(data.shippingSn, shipperCode).then(response => {
        this.shippingInfo.form['traces'] = response.data.traces
        this.shippingInfo.show = true
      })

    },
    openSendOutForm(id) {
      this.shipping.id = id
      this.shipping.show = true
    },
    sendOut() {
      this.$confirm(this.$t('common.optionConfirm'), this.$t('common.tooltip'), {
        confirmButtonText: this.$t('button.submit'),
        cancelButtonText: this.$t('button.cancel'),
        type: 'warning'
      }).then(() => {
        orderApi.sendOut(this.shipping.id, this.shipping.idExpress, this.shipping.shippingSn).then(response => {
          this.fetchData()
          this.shipping.show = false
          this.$message({
            message: '发货成功',
            type: 'success'
          })
        })
      }).catch(() => {
      })
    },
    viewLog(id) {
      this.logs = []
      this.logVisible = true
      orderApi.getLogs(id).then(response => {
        this.logs = response.data
      })
    },
    addComment(id) {
      this.$prompt('请输入备注信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        orderApi.addComment(id, value)
      }).catch(() => {
      })
    },
    exportXls() {
      orderApi.exportXls(this.listQuery).then(response => {
        window.location.href = getApiUrl() + '/file/download?fileName=' + response.data.realFileName
      })
    },
    getGoodsCreatList() {
      settlementApi.getSysUser().then(response => {
        let a = {
          id: 0,
          nameAndAccount: "全部"
        }
        this.goodsCreatList.push(a)
        //console.log("获取创建者ID", response);
        for (let i = 0; i < response.data.length; i++) {
          const e = response.data[i];
          let tmp = {
            id: e.id,
            nameAndAccount: e.nameAndAccount,
            name: e.name,
          }
          this.goodsCreatList.push(tmp)
        }

        this.goodsCreatSelect = 0;
        if (this.goodsCreatSelect == 0 && this.initFlag) {
          let thisUserId = this.$store.state.user.profile.id
          for (let i = 0; i < this.goodsCreatList.length; i++) {
            const e = this.goodsCreatList[i];
            if (e.id == thisUserId) {
              this.listQuery.goodsCreat = e.id
              this.goodsCreatSelect = e.id
              break
            } else {
              this.listQuery.goodsCreat = 0
            }
          }
          if (this.listQuery.goodsCreat == 0) {
            this.fetchData()
          }
          this.initFlag = false
        }
      })
    },
    // 退款处理
    refundHandler(row) {
      this.refundInfo.show = true
      this.refundInfo.form.username = row.user.nickName
      this.refundInfo.form.orderSn = row.orderSn
      this.refundInfo.form.statusName = row.statusName
      this.refundInfo.form.status= row.status
      this.refundInfo.form.totalPrice = this.formatPrice(row.totalPrice)
      this.refundInfo.form.realPrice = this.formatPrice(row.realPrice)
    },
    // 拒绝退款
    refuseRefund() {
      this.$confirm('确定拒绝退款?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        orderApi.refuseRefund(this.refundInfo.form.orderSn).then(response => {
          console.log("拒绝退款响应：", response);
          this.fetchData()
        }).catch(() => {
          console.log("拒绝退款异常");
        })
        this.refundInfo.show = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消拒绝退款'
        });
      });
    },
    // 同意退款
    agreeRefund() {
      this.$confirm('确定退款?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        orderApi.agreeRefund(this.refundInfo.form.orderSn).then(response => {
          console.log("退款响应：", response);
          this.$message({
            type: 'success',
            message: '同意退款成功'
          });
          this.fetchData()
        }).catch(() => {
          console.log("退款异常");
        })
        this.refundInfo.show = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消退款'
        });
      });
    }
  }
}
